package com.sophialex.health.record.views.pregnant;

import java.util.Date;

import org.eclipse.swt.widgets.Composite;

import com.sophia.common.pojo.SophiaCommonPojo;
import com.sophia.common.xmlbean.pojo.postpartum42check.BaseInfoXMLPojo;
import com.sophia.common.xmlbean.pojo.postpartum42check.FileNumXMLPojo;
import com.sophia.common.xmlbean.pojo.postpartum42check.PersonNameXMLPojo;
import com.sophia.common.xmlbean.util.BaseXMLPojo;
import com.sophia.common.xmlbean.util.PojoXMLUtil;
import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.common.platform.util.CustomGenerator;
import com.sophialex.health.person.vo.SoapService;
import com.sophialex.health.person.vo.SoapServiceDetail;
import com.sophialex.health.record.Messages;
import com.sophialex.health.record.common.Constants;
import com.sophialex.health.record.common.util.DataSource;
import com.sophialex.health.record.common.util.EventType;
import com.sophialex.health.record.common.util.HealthRecordListenerRegister;
import com.sophialex.health.record.common.utils.CommonUtils;
import com.sophialex.health.record.ui.pregnant.Postpartum42DayCheckUI;
import com.sophialex.health.record.views.BaseView;
import com.sophialex.health.system.frame.dialog.DialogManager;
import com.sophialex.health.system.frame.util.ViewUitl;
/**
 * 产后 42天检查
 * <p>Title:Health</p>
 * <p>PostpartumView</p>
 * 
 * Alex May 26, 2011 8:41:39 PM create
 * @author <a href='wanghao924@126.com'>Alex</a>
 * QQ: 873987060;
 * @version 1.0
 */
public class Postpartum42DayCheckView extends Postpartum42DayCheckUI implements BaseView{

	public static final String ID = "com.sophialex.health.record.views.pregnant.Postpartum42DayCheckView"; //$NON-NLS-1$
    
	protected SoapService soapService;
	
	protected SoapServiceDetail soapServiceDetail;
	
	public void createPartControl(Composite parent) {
		super.createPartControl(parent);
		setActionControl();
		ap.process();
	}
	
	@Override
	protected void doAdd() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void doCancel() {
		switchView(Postpartum42DayCheckUIState.SHOW_VIEW);
	}

	@Override
	protected void doDel() {
		if(DialogManager.invokeConfirmDlg(Messages.getString("CommonInfo.3"))){
			boolean flag = DataSource.soapService.delete(soapService);
			if(flag){
				DialogManager.invokeInfoDlg(Messages.getString("CommonInfo.4")); 
				clearView();
				updateRightView();
				ViewUitl.close(Postpartum42DayCheckView.ID);
			}else{
				DialogManager.invokeInfoDlg(Messages.getString("CommonInfo.5")); 
			}
		}
		
	}

	@Override
	protected void doEdit() {
		switchView(Postpartum42DayCheckUIState.EDIT_VIEW);
	}

	@Override
	protected void doPrint() {
		
	}

	@Override
	protected void doSave() {
		boolean flag = false;
		if(soapService == null){
			SophiaCommonPojo commonPojo = CommonUtils.getCommonPojo();
			soapService = new SoapService();
			soapService.setId(CustomGenerator.generator());
			soapService.setBusType(Constants.CH42CHECK);   //业务类别
			soapService.setComsumerId(SuperContext.currentPerson.getId());  //服务对象
			soapService.setSoapCustomId(SuperContext.currentPerson.getId());  //服务对象
			soapService.setProviderId(SuperContext.currentUser.getId()); //服务提供者
			soapService.setIsMainService(Constants.YES);
			soapService.setServiceTime(new Date());   //服务时间
			soapService.setCommonPojo(commonPojo);
			
	        BaseInfoXMLPojo baseInfo = new BaseInfoXMLPojo();
	        PersonNameXMLPojo personName = new PersonNameXMLPojo();
	        FileNumXMLPojo fileNum = new FileNumXMLPojo();
	        personName.setValue(SuperContext.currentPerson.getName());
	        fileNum.setValue(SuperContext.currentPerson.getArchiveSerial());
	        baseInfo.setFileNum(fileNum);
	        baseInfo.setPersonName(personName);
	        postpartum42DayCheck.setBaseInfo(baseInfo);
	        
			soapServiceDetail = new SoapServiceDetail();
			soapServiceDetail.setServiceId(soapService.getId());
			soapServiceDetail.setContent(PojoXMLUtil.pojoToXML(postpartum42DayCheck));
			soapServiceDetail.setCommonPojo(commonPojo);
			flag = DataSource.soapService.saveSoapService(soapService,soapServiceDetail);
		}else{
			ap.updateModel();
			soapServiceDetail.setContent(PojoXMLUtil.pojoToXML(postpartum42DayCheck));
			SophiaCommonPojo common = CommonUtils.getCommonPojo();
			common.setUpdateTime(new Date());
			soapServiceDetail.setCommonPojo(common);
			soapService.setCommonPojo(common);
			flag = DataSource.soapService.updateSoapService(soapService,soapServiceDetail);
		}
		updateRightView();
		if(flag){
			DialogManager.invokeInfoDlg(Messages.getString("CommonInfo.0")); 
			switchView(Postpartum42DayCheckUIState.SHOW_VIEW);
		}else{
			DialogManager.invokeInfoDlg(Messages.getString("CommonInfo.1")); 
			switchView(Postpartum42DayCheckUIState.ADD_VIEW);
		}
		
	}

	@Override
	public void setFocus() {
		 
		
	}

	void switchView(Postpartum42DayCheckUIState state) {
		viewstate = state;
		setActionControl();
	}

	@Override
	public void refershView(BaseXMLPojo baseXmlPojo,SoapService soapService,SoapServiceDetail soapServiceDetail) {
		ap.updateUI(postpartum42DayCheck, baseXmlPojo);
		this.soapService = soapService;
		this.soapServiceDetail = soapServiceDetail;
		switchView(Postpartum42DayCheckUIState.SHOW_VIEW);
	}
	
	public void clearView(){
		soapService = null;
		soapServiceDetail = null;
		ap.updateUI(postpartum42DayCheck, null);
		switchView(Postpartum42DayCheckUIState.ADD_VIEW);
	}
	
	/**
	 * 刷新右边服务记录视图 
	 * @author Alex 
	 * May 27, 2011 8:22:27 PM create
	 */
	public void updateRightView(){
		HealthRecordListenerRegister.getInstance().postEvent(null, EventType.REFRESH_ONLY);
	}
}
